package com.hotdog.service;


import com.hotdog.feign.SystemFeign;


import com.hotdog.pojo.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 封装用户登录时 信息的获取与封装
 * 在页面上点击登录按钮时 会调用这个对象的  loadUserByUsername方法
 * UserDetails封装用户信息
 *
 * @author dell
 */
@Slf4j
@Service
public class LoginServiceImpl implements UserDetailsService {
    @Autowired
    private SystemFeign systemFeign;


    @Override

    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        //1.基于用户名查询用户信息(用户名 用户状态 密码)
        log.error(username);

        User user = systemFeign.selectUserByUsername(username);


        String password = user.getPassword();

        log.info(user.toString());
        List<String> list = systemFeign.selectUserPermissions(user.getId());
        log.error(list.toString());
        String[] strings = list.toArray(new String[]{});

        //2.对用户信息进行封装
        List<GrantedAuthority> authorityList = AuthorityUtils.createAuthorityList(strings);
        return new org.springframework.security.core.userdetails.User(username, password, authorityList);
    }
}
